package com.atguigu.gmall.gmallpublisher.controller;

import com.atguigu.gmall.gmallpublisher.server.SugarService;
import com.atguigu.gmall.gmallpublisher.utils.DateFormatUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

/**
 * ClassName: SugarController
 * Package: com.atguigu.gmall.gmallpublisher.controller
 * Description:
 *
 * @Author ChenJun(有志男青年)
 * @Create 2023/5/13 11:57
 * @Version 1.0
 */
@RestController
@RequestMapping("/sugar")
public class SugarController {

    @Autowired
    private SugarService sugarService;

    @RequestMapping("/test1")
    //@ResponseBody
    public String test01() {
        System.out.println("111111111111");
        return "success";
    }

    @RequestMapping("/test02")
    public String test02(@RequestParam("nn") String name,
                         @RequestParam(value = "age", defaultValue = "18") int age) {
        System.out.println(name + ":" + age);
        return "success";
    }

    /**
     *     查询支付总额
     * @param date
     * @return
     */
    @RequestMapping("/gmv")
    public String getGmv(@RequestParam(value = "date", defaultValue = "0") int date){

        if (date == 0) {
            date = getToday();
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": " + sugarService.getGmv(date) + "\n" +
                "}";
    }


    @RequestMapping("/trademark")
    public String getGmvByTm(@RequestParam(value = "date",defaultValue = "0") int date, @RequestParam(value = "limit",defaultValue = "5") int limit){

        if (date == 0){
            date = getToday();
        }

        //查询数据
        Map<String, BigDecimal> gmvByTm = sugarService.getGmvByTm(date, limit);

        Set<String> tmName = gmvByTm.keySet();
        Collection<BigDecimal> gmvs = gmvByTm.values();

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [\"" +StringUtils.join(tmName, "\",\"") + "\"],\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"GMV:\",\n" +
                "        \"data\": [\n" +
                StringUtils.join(gmvs, ",") + "\n" +
                "]\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";


    }
    private int getToday() {
        return Integer.parseInt(DateFormatUtil.toDate(System.currentTimeMillis()).replace("-", ""));
    }
}
